package com.hbnu.jdbc;

import java.sql.*;

/**
 * @author kittychen
 * @since 2025-10-24 8:53
 */
public class JdbcDemo05 {

    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        String url = "jdbc:mysql://127.0.0.1:3306/class2311?serverTimezone=GMT&useSSL=false&characterEncoding=utf8";
        String name = "root";
        String pwd = "123456";
        try {
            connection = DriverManager.getConnection(url, name, pwd);

            // 自己完成修改、删除、查询操作
            String sql = "insert into tb_user values(?, ?, ?, ?)";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setInt(1, 4);
            preparedStatement.setString(2, "王五");
            preparedStatement.setString(3, "123456");
            preparedStatement.setDouble(4, 1000);
            int rows = preparedStatement.executeUpdate();

            System.out.println(rows >= 1 ? "插入成功" : "插入失败");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
